package framework.account;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import framework.customer.ICustomer;
import framework.observer.BaseObserver;

/**
 * This class is responsible to provide basic operations on account
 *
 * @author Abdulaziz Ergashev
 */
public abstract class BaseAccount extends Observable implements IAccount {
    public BaseAccount(String type, String accountNumber) {
        this.type = type;
        this.accountNo = accountNumber;
        balance = 500;
    }

    protected String type;
    protected String accountNo;
    protected float balance;
    protected String expDate;
    protected ICustomer customer;
    /**
     * @uml.property name="transactionHistory"
     * @uml.associationEnd multiplicity="(0 -1)" elementType="framework.account.IEntry"
     */
    private List<IEntry> transactionHistory;

    /*
     * Adds Transactions and notifies its observers about change
     * @see framework.account.IAccount#addTransaction(framework.account.IEntry)
     */
    public void addTransaction(IEntry tr) {
        if (transactionHistory == null) {
            transactionHistory = new ArrayList<IEntry>();
        }
        transactionHistory.add(tr);
        setChanged();
        //passes current transaction to observers as an parameter
        this.notifyObservers(tr);
    }


    public void setCustomer(ICustomer cust) {
        this.customer = cust;

    }


    public ICustomer getCustomer() {
        return customer;
    }


    public String getType() {
        return type;
    }


    public void addInterest() {
        balance += balance * getInterestRate();
    }

    public boolean deposit(float amount) {
        balance += amount;
        return true;
    }

    public boolean withdraw(float amount) {
        balance -= amount;
        return true;
    }


    /**
     * @return
     * @uml.property name="balance"
     */
    public float getBalance() {
        return balance;
    }

    public void updateBalance(float balance) {
        this.balance = balance;
    }

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    public String getExpDate() {
        return expDate;
    }

    public void setExpDate(String expDate) {
        this.expDate = expDate;
    }
	/*
	  adds observers that subscribed to this account to its list
	 * @see framework.account.IAccount#addBaseObserver(framework.observer.BaseObserver)
	 */
    public <T extends BaseAccount, E extends Transaction> void addBaseObserver(BaseObserver<T, E> bo) {
        super.addObserver(bo);
    }
}